<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="/common/header.jsp"%>
<div id="menuTree">
    <div id="ztreeObj" class="ztree"></div>
</div>
<script>

    function savecustomProfit(urls){
        /*表单提交*/
        $("#customProfitForm").form('submit', {
            url: urls,
            onSubmit: checkForm,
            success: function(data){
                if(data === "success"){
                    window.history.back(-1);
                }else{
                    $.jpmessage.error("会员分润配置保存失败！");
                }
            }
        });

    }

    $(document).ready(function() {
        var ztreeObj;
        /*选择按钮*/
        $("#selelctBtn").click(function(){
        	var option = {url:'../insinf/getDirectInsInfTree.do'};
            ztreeObj = showInsTree(selected, null, option);
        });

        /*确定按钮回调*/
        function selected(){
            var nodes = ztreeObj.getSelectedNodes();
            if(nodes && nodes.length > 0){
                $("#ins_name").textbox("setValue", nodes[0].NAME);
                $("#ins_uuid").val(nodes[0].ID);
            }
        }

        /*/!*表单提交*!/
		$("#customProfitForm").form({
			url: "saveCustomProfit.do",
			onSubmit: checkForm,
			success: function(data){
				if(data === "success"){
					window.history.back(-1);
				}else{
					$.jpmessage.error("会员分润配置保存失败！");
				}
			}
		});*/
    });
    
    /**
     *	检验form表单的内容合法性
     */
    function checkForm(){
    	if(!$("#ins_uuid").val()){
    		$.jpmessage.warning("请选择所属机构！");
    		return false;
    	}
    	/* if(!$("#customProfitForm").form("validate")){
			return false;
		}; */
		//分润方式
		var profit_mode = $("#profit_mode").val();
		//分润类型
		var profit_type = $("#profit_type").val();
		var indirect_comm = $("#indirect_comm").val();
		var direct_comm = $("#direct_comm").val();
		var cur_comm = $("#cur_comm").val();
		if(indirect_comm == null || indirect_comm == ''){
			indirect_comm = 0.00;
		}else{
			indirect_comm = parseFloat(indirect_comm);
		}
		
		if(direct_comm == null || direct_comm == ''){
			direct_comm = 0.00;
		}else{
			direct_comm = parseFloat(direct_comm);
		}
		
		if(cur_comm == null || cur_comm == ''){
			cur_comm = 0.00;
		}else{
			cur_comm = parseFloat(cur_comm);
		}
		
		if(profit_mode == '1' && profit_type == '3'){
			$.jpmessage.warning("分润方式为\"固定金额\"时，分润类型不能为\"交易分润\"！");
			return false;
		}
		if(profit_mode == '2' && profit_type != '3'){
			$.jpmessage.warning("分润方式为\"比例\"时，分润类型只能为\"交易分润\"！");
			return false;
		}
		if(profit_mode == '2'){
			if(indirect_comm + direct_comm + cur_comm > 100){
				$.jpmessage.warning("当前会员、直接推荐人、间接推荐人获利比例之和不能超过100！");
				return false;
			}
		}
    	var isExist = false;
		$.jpajax({
			url: "checkInsProfitStatus.do",
			data: {custom_profit_uuid:$("#custom_profit_uuid").val(),
					ins_uuid: $("#ins_uuid").val(), 
					indirect_custom_level:$("#indirect_custom_level").val(), 
					direct_custom_level:$("#direct_custom_level").val(),
					cur_custom_level:$("#cur_custom_level").val(),
					profit_type:$("#profit_type").val()},
			success: function(result){
				if(result == 'error'){
					$.jpmessage.warning("该机构下分润配置已有此组合选项，不能再次配置！");
				}else if(result == 'success'){
					isExist = true;
				}
			}
		});
		return isExist;
    }

</script>
<div style="margin:20px 0;"></div>
<div class="easyui-panel" >
    <div style="padding:10px 60px 20px 60px">
        <form id="customProfitForm"  name="customProfitForm" method="post">
            <input id="custom_profit_uuid" type="hidden" name="custom_profit_uuid" value="${customProfitEntity.custom_profit_uuid}"/>
            <input id="rec_id" type="hidden" name="rec_id" value="${customProfitEntity.rec_id}"/>
            <table cellpadding="5" style="width:100%">
                <tr>
                    <td>所属机构:</td>
                    <td>
                        <input id="ins_uuid" type="hidden" name="ins_uuid" value="${customProfitEntity.ins_uuid}"/>
                        <input class="easyui-textbox" type="text" readonly="readonly" id="ins_name" name="ins_name" value="${ins_name}"/>
                        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-search" id="selelctBtn">选择</a>
                    </td>
                </tr>
                <tr>
                <tr>
                    <td>当前会员级别:</td>
                    <td><code:codeSelect name="cur_custom_level" id="cur_custom_level" code_type="CUR_CUSTOM_LEVEL" showSelect="false" selectId="${customProfitEntity.cur_custom_level }"></code:codeSelect></td>
                </tr>
                    <td>直接推荐人会员级别:</td>
                    <td><code:codeSelect name="direct_custom_level" id="direct_custom_level" code_type="DIRECT_CUSTOM_LEVEL" showSelect="false" selectId="${customProfitEntity.direct_custom_level }"></code:codeSelect></td>
                </tr>
                <tr>
                    <td>间接推荐人会员级别:</td>
                    <td><code:codeSelect name="indirect_custom_level" id="indirect_custom_level" code_type="INDIRECT_CUSTOM_LEVEL" showSelect="false" selectId="${customProfitEntity.indirect_custom_level }"></code:codeSelect></td>
                </tr>
                <tr>
                    <td>分润方式:</td>
                    <td><code:codeSelect name="profit_mode" id="profit_mode" code_type="PROFIT_MODE" showSelect="false" selectId="${customProfitEntity.profit_mode }"></code:codeSelect></td>
                </tr>
                <tr>
                    <td>分润类型:</td>
                    <td><code:codeSelect name="profit_type" id="profit_type" code_type="PROFIT_TYPE" showSelect="false" selectId="${customProfitEntity.profit_type }"></code:codeSelect></td>
                </tr>
                <tr>
                    <td>当前会员获利金额(比例):</td>
                    <td><input class="easyui-numberspinner" id="cur_comm" name="cur_comm" data-options="required:true,precision:2,decimalSeparator:'.',min:0,max:9999999999.99" value="${customProfitEntity.cur_comm}"></input></td>
                </tr>
                <tr>
                    <td>直接推荐人获利金额(比例):</td>
                    <td><input class="easyui-numberspinner" id="direct_comm" name="direct_comm" data-options="required:true,precision:2,decimalSeparator:'.',min:0,max:9999999999.99" value="${customProfitEntity.direct_comm}"></input></td>
                </tr>
                <tr>
                    <td>间接推荐人获利金额(比例):</td>
                    <td><input class="easyui-numberspinner" id="indirect_comm" name="indirect_comm" data-options="required:true,precision:2,decimalSeparator:'.',min:0,max:9999999999.99" value="${customProfitEntity.indirect_comm}"></input></td>
                </tr>
            </table>

            <div style="text-align:center;padding:5px">
	            <c:if test="${insLevel == '1' ||  (insLevel != '1' && insUuid != customProfitEntity.ins_uuid)}">
	                <jiepos:buttonDetailTag menuCode="${menuCode}"  hideBack="true"  formId="customProfitForm"></jiepos:buttonDetailTag>
	            </c:if>
	            <a href="javascript:void(0)" class="easyui-linkbutton btn" iconCls="icon-back" onclick="back()">返回</a>
            </div>
        </form>
    </div>
</div>
